<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@page import="java.util.*, java.sql.*" %>



<%
	String userNameParam = request.getParameter("username");
	String passwordParam = request.getParameter("password");
	String passwordVerifyParam = request.getParameter("passwordVerify");
	String emailParam = request.getParameter("email");
	
	String userError = "";
	String passwordError = "";
	String emailError = "";

	Class.forName("org.postgresql.Driver");
	Connection connect = DriverManager.getConnection("jdbc:postgresql://localhost/access_control?user=postgres&password=admin");
	
	if(userNameParam != null)
	{
			
		PreparedStatement stmt;
		ResultSet rset;
		stmt = connect.prepareStatement("SELECT * FROM users");
		rset = stmt.executeQuery();
		Vector<String> dbuserID = new Vector<String>();
		Vector<String> dbusernames = new Vector<String>();
		Vector<String> dbemails = new Vector<String>();
		while (rset.next())
		{
				dbuserID.add(rset.getString("user_id"));
				dbusernames.add(rset.getString("user_name"));
				dbemails.add(rset.getString("email"));
		}
		
		if(dbusernames.contains(userNameParam))
		{
				userError = "Duplicate Username, please choose another";
		}
		if(!passwordParam.equals(passwordVerifyParam))
		{
			passwordError = "Passwords are not identical";
		}
		if(dbemails.contains(emailParam))
		{
			emailError = "You have already registered! Please log in with the correct username and password";
		}
	}
	
	
	connect.setAutoCommit(false);

	
	
	if(userNameParam != null && userError.length() == 0 && passwordError.length() == 0 && emailError.length() == 0)
	{
		
		PreparedStatement prepStmt;
		PreparedStatement prepStmt2;
		PreparedStatement query;
		ResultSet rset;
		%>we get into the commit area<%
		
		try{
			
			
			prepStmt = connect.prepareStatement("INSERT INTO users (user_name, password, email) VALUES ('"+userNameParam+"', md5('"+passwordParam+"'), '"+emailParam+"');");
			prepStmt.executeUpdate();
			
			query = connect.prepareStatement("SELECT last_value FROM users_user_id_seq");
			rset = query.executeQuery();
			String userID = "";
			rset.next();
			userID = rset.getString("last_value");
			
			
			prepStmt2 = connect.prepareStatement("INSERT INTO user_roles (user_ref, user_name, role) VALUES ("+userID+", '"+userNameParam+"','student');");
			prepStmt2.executeUpdate();
			
			connect.commit();
			
			response.sendRedirect("Success.html");
		} catch (Exception e) {
			
			%>bad sql. no commit<%
		}
	
		
	}
	
	
	
		

%>



<html>
	<body>
		<form name="register" method="GET" action="RegisterUser.jsp">
			<table>
				<tr>
					<td>Name</td>
					<td>
						<input name="username" type="text"/>
					</td>
				</tr>
				<tr>
					<td>Password</td>
					<td>
						<input maxlength="20" size="12" name="password" type="password"/>
					</td>
				</tr>
				<tr>
					<td>Re-Type Password</td>
					<td>
						<input maxlength="20" size="12" name="passwordVerify" type="password"/>
					</td>
				</tr>
				<tr>
					<td>Email</td>
					<td>
						<input name="email" type="text"/>
					</td>
				</tr>
			</table>
			<input type="submit" value="register"/>
		</form>
		<%= userError %><br>
		<%= passwordError %><br>
		<%= emailError %><br>
		<a href="login.jsp" > Back to login </a><br>
		
	</body>
</html>